This page explains the concept and terminology related to array size.
An array is a collection of data organized on a rectangular grid that can span across multiple dimensions. Except function handles, data of all types are stored in arrays. For examples,
Given an array A, the length of the i-th dimension is given by size(A, i). This is illustrated in the figure below, where A has 3 dimensions with lengths greater than one.

It is important to understand that SIMO sees this as an array with more than 3 dimensions, where 4th and higher dimensions all have unit lengths. That is why, in the example below, calling size(A, i) with i greater than 3 returns all ones:
A = ones(2,3,4);
for i = 1:6
size(A, i)
end
ans =
2.0000
ans =
3.0000
ans =
4.0000
ans =
1.0000
ans =
1.0000
ans =
1.0000
A dimension having a unit length is called a singleton dimension. In the above example, the
Given two arrays, namely, A and B. When a binary operation is performed on the d-th dimension, at least one of the following conditions must hold. Otherwise, a size-mismatch error will be thrown.
size(A, d) == size(B, d),size(A, d) = 1size(B, d) = 1 The first condition ensures that each element of A can be operated on the element of B at the same position. If A and B have different lengths in the d-th dimension, one of them should have a unit length along the d-th dimension (i.e., the second and third conditions), so that the dimension can be expanded to match the length of the other array.
If at least one of the above 3 conditions holds for each d in 1, 2, ..., max(ndims(A), ndims(B)), then A and B are said to have compatible sizes. If two arrays have compatible sizes, a binary operation can be performed on these arrays.
For examples, each pair of the following arrays has compatible sizes:
1 and ones(3,4).
1 will be expanded to have size [3,4] in a binary operation.1 and ones(3,4,5).
1 will be expanded to have size [3,4,5] in a binary operation.ones(2,3) and ones(2,3,2).
rand(1,3) and zeros(2,3).
sizeThe function size returns the "size vector" of an array. More precisely, by size vector, we mean
This definition is illustrated by the following examples:
size([]) gives [0, 0] as seen from Item 1 above.size(1) gives [1, 1] as seen from Item 2 above.size([1; 2; 3]) gives [3, 1] as seen from Item 3 above with size([1, 2, 3]) gives [1, 3] as seen from Item 4 above with size(ones(2,3)) gives [2,3] as seen from Item 5 above with size(ones(2,3,4)) gives [2,3,4] as seen from Item 5 above with size(ones(2,3,1,1,1,4)) gives [2,3,1,1,1,4] as seen from Item 5 above with ndimsThe number returned by ndims(A) is commonly referred to as the "number of dimensions" of A. More precisely,
ndims(A) gives 2 when A is empty, a scalar or a vector;ndims(A) gives the highest non-singleton dimension of A.lengthThe length of the i-th dimension refers to size(A, i), i.e., the dimension length. It should be noted that the length of an array means the length of the longest dimension of the array. The length of an array can be obtained by the function length(). For example, length(ones(2,3,4)) gives 4.
numelThe number of elements in the array A can be obtained by numel(A). For examples, the numel of an empty array is zero, and the numel of a scalar is 1.